package xmldatabase;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Xmlconnection {

    SAXReader reader = new SAXReader();
    File file = new File("D:\\学习\\XML\\新闻系统\\news1009_01.xml");

    {
        Document document = null;
        try {
            document = reader.read(file);
        } catch (DocumentException ex) {
        }
        Element bookStore = document.getRootElement();
        Iterator<Element> it = bookStore.elementIterator();
        int i = 0;

        while (it.hasNext()) {
            i++;
            System.out.println("=============开始遍历第" + i + "条新闻==========");
            Element element = it.next();
            List<Attribute> newsAttrs = element.attributes();
            for (Attribute attr : newsAttrs) {
                System.out.println("属性名：" + attr.getName() + "-----属性值：" + attr.getValue());
            }
            Iterator<Element> childNode = element.elementIterator();
            while (childNode.hasNext()) {
                Element child = childNode.next();
                System.out.println("节点名：" + child.getName() + "-----节点值：" + child.getStringValue());
            }
                               
            System.out.println("=============结束遍历第" + i + "条新闻==========");
        }
    }
}
